package com.java2104.controller;

import com.java2104.entity.LoginUser;
import com.java2104.entity.UserMessage;
import com.java2104.service.LoginUserService;
import com.java2104.service.UserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@Controller

public class UserMessageController {

    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private LoginUserService loginUserService;

    @GetMapping({"/user_add","/user_add.html"})
    public String userAdd(Model model){
        model.addAttribute("loginUserList",loginUserService.list());
        return "user_add";
    }

    @PostMapping("/user_add")
    public String userAdd(Model model, LoginUser loginUser){
        if(userMessageService.saveUserMessage(loginUser.getUserMessage())>0 && loginUserService.saveLoginUser(loginUser)>0){
            model.addAttribute("ret_msg","添加成功");
        }else {
            model.addAttribute("ret_msg","添加失败");
        }
        return userAdd(model);
    }

    @InitBinder//会在处理方法执行前，将参数类型进行判断和转化
    public void initBinder(WebDataBinder webDataBinder, String height) {
        //负责将客户端发送的请求参数中的 "yyyy-MM-dd" 的日期转换成 java.util.Date的对象
        CustomDateEditor customDateEditor
                = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false);
        webDataBinder.registerCustomEditor(java.util.Date.class, customDateEditor);
        webDataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
        if (height==null || height.equals("")) {
            webDataBinder.setDisallowedFields("height");
        }
    }

    @RequestMapping("/userMessage/list")
    @ResponseBody
    public List<UserMessage> userMessageList(String username,Integer sex){
        return userMessageService.list(username,sex);
    }
    //
    /*@PostMapping(path = "/userMessage/list", consumes = MediaType.APPLICATION_JSON_VALUE)
    public List<UserMessage> userMessageList(@RequestBody  UserMessage userMessage){
        return userMessageService.list(userMessage.getUsername(),userMessage.getSex());
    }*/

    @RequestMapping("/userMessage/one/{id}")
    @ResponseBody
    public UserMessage one(@PathVariable Integer id){
        return  userMessageService.getById(id);
    }

    @RequestMapping(path = "/userMessage/update",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String update(@RequestBody UserMessage userMessage){
        return userMessageService.updateMessage(userMessage)?"success":"fail";
    }

    @RequestMapping("/userMessage/delete/{id}")
    public String delete(@PathVariable Integer id){
        userMessageService.deleteMessageById(id);
        return "redirect:/user_list";
    }
}
